package com.meishij.fragment.finddetail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.meishij.R;
import com.meishij.adapter.FindShiCaiRecyclerAdapter;
import com.meishij.entry.FindShiCaiEntry;
import com.meishij.server.FindServer;
import com.meishij.xml.FindDetailXmlUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by 阿飞 on 2016/10/13.
 */

public class FindShiCaiActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = FindShiCaiActivity.class.getSimpleName();
    private RecyclerView recycler_find_detail_shicai;
    private View currentIndex;
    private View shuCai;
    private View shuiGuo;
    private View rouLei;
    private View wuGu;

    private Retrofit retrofit;
    private FindShiCaiRecyclerAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_detail_shicai);
        initRetrofit();
        initView();
    }

    private void initView() {
        View back = findViewById(R.id.iv_find_detail_header_back);
        back.setOnClickListener(this);

        TextView tv_find_detail_header_title = (TextView) findViewById(R.id.tv_find_detail_header_title);
        tv_find_detail_header_title.setText("当季节食材");

        recycler_find_detail_shicai = (RecyclerView) findViewById(R.id.recycler_find_detail_shicai);
        LinearLayoutManager layouManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recycler_find_detail_shicai.setLayoutManager(layouManager);
        adapter = new FindShiCaiRecyclerAdapter(this, null);
        recycler_find_detail_shicai.setAdapter(adapter);

        currentIndex = new View(this);
        shuCai = findViewById(R.id.tv_find_detail_shicai_shuCai);
        shuCai.setOnClickListener(this);
        shuiGuo = findViewById(R.id.tv_find_detail_shicai_shuiGuo);
        shuiGuo.setOnClickListener(this);
        rouLei = findViewById(R.id.tv_find_detail_shicai_rouLei);
        rouLei.setOnClickListener(this);
        wuGu = findViewById(R.id.tv_find_detail_shicai_wuGu);
        wuGu.setOnClickListener(this);

        changeCurrentIndex(shuCai);
    }

    private void initRetrofit() {
        retrofit = new Retrofit.Builder()
                .baseUrl(FindServer.BaseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }

    private void requestNetData(String sct) {
        FindServer findServer = retrofit.create(FindServer.class);

        Call<String> call = findServer.getShiCaiResult(sct);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String xml = response.body();
                FindShiCaiEntry findShiCaiEntry = FindDetailXmlUtils.xml2FindShiCaiEntry(xml);
                adapter.addAll(findShiCaiEntry.getItems());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(FindShiCaiActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 改变 recycler 的显示的内容
     *
     * @param toView 跳转的 View
     */
    public void changeCurrentIndex(View toView) {
        if (currentIndex != toView) {
            currentIndex.setSelected(false);
            currentIndex = toView;
            toView.setSelected(true);
            switch (toView.getId()) {
                case R.id.tv_find_detail_shicai_shuCai:
                    requestNetData("2");
                    break;
                case R.id.tv_find_detail_shicai_shuiGuo:
                    requestNetData("1");
                    break;
                case R.id.tv_find_detail_shicai_rouLei:
                    requestNetData("3");
                    break;
                case R.id.tv_find_detail_shicai_wuGu:
                    requestNetData("4");
                    break;
            }
            adapter.clear();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_find_detail_header_back:
                finish();
                break;
            case R.id.tv_find_detail_shicai_shuCai:
                changeCurrentIndex(shuCai);
                break;
            case R.id.tv_find_detail_shicai_shuiGuo:
                changeCurrentIndex(shuiGuo);
                break;
            case R.id.tv_find_detail_shicai_rouLei:
                changeCurrentIndex(rouLei);
                break;
            case R.id.tv_find_detail_shicai_wuGu:
                changeCurrentIndex(wuGu);
                break;
        }
    }
}
